package UIStuff;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import java.awt.Font;
import java.awt.print.Book;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.JTextPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JSplitPane;

public class ClassUI {
	
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassUI window = new ClassUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws FileNotFoundException 
	 */
	public ClassUI() throws FileNotFoundException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws FileNotFoundException 
	 */
	private void initialize() throws FileNotFoundException {
		frame = new JFrame();
		frame.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JTree tree;
		XMLDecoder decoder = new XMLDecoder( new BufferedInputStream( new FileInputStream( "tree.xml" ) ) );

		TreeModel model = ( TreeModel )decoder.readObject();
		decoder.close();
		tree = new JTree(model);
/*		DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
		tree = new JTree(top);
		tree.setBounds(111, 63, 99, 243);
		createNodes(top);*/
		
		JScrollPane scrollPane = new JScrollPane(tree);
		Dimension minSize = new Dimension(100,50);
		scrollPane.setMaximumSize(minSize);
		
		JTextPane textPane = new JTextPane();
		textPane.setMaximumSize(minSize);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(12, 11, 370, 215);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(150);
		frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("148px:grow"),},
			new RowSpec[] {
				RowSpec.decode("fill:145px:grow"),}));
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, textPane);
		frame.getContentPane().add(splitPane, "1, 1, fill, fill");
	
	/*    XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream(new FileOutputStream("tree.xml")));
	    encoder.writeObject( tree.getModel() );
	    encoder.close();*/

	}
	
	private void createNodes(DefaultMutableTreeNode top) {
	    DefaultMutableTreeNode category = null;
	    DefaultMutableTreeNode book = null;
	    DefaultMutableTreeNode bookp = null;
	    
	    category = new DefaultMutableTreeNode("Books for Java Programmers");
	    top.add(category);
	    
	    //original Tutorial
	    book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics");
	    category.add(book);
	    
	    //Tutorial Continued
	    book = new DefaultMutableTreeNode(new Object());
	    category.add(book);
	    bookp = new DefaultMutableTreeNode(new Object());
	    book.add(bookp);
	    //Swing Tutorial
	    book = new DefaultMutableTreeNode(new Book());
	    category.add(book);

	    //...add more books for programmers...

	    category = new DefaultMutableTreeNode("Books for Java Implementers");
	    top.add(category);


	    //Language Spec
	    book = new DefaultMutableTreeNode("The Java Language Specification");
	    category.add(book);
	}
}
